package cn.gson.school.controller.jiaoxue;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.*;

import cn.gson.school.dao.jiaoxue.ProjectDao;
import cn.gson.school.dao.jiaoxue.TeamDao;
import cn.gson.school.dao.xitong.UserInfoDao;
import cn.gson.school.entity.jiaoxue.FangTanEntity;
import cn.gson.school.entity.jiaoxue.ProjectEntity;
import cn.gson.school.entity.jiaoxue.TeamEntity;
import cn.gson.school.entity.xitong.UserInfoEntity;
import cn.gson.school.mappers.jiaoxue.ProjectMappers;

@Controller
@RequestMapping("/project")
public class ProjectController {

	@Autowired
	private ProjectMappers projectMappers;

	@Autowired
	private ProjectDao projectDao;

	@Autowired
	private TeamDao teamDao;
	
	@Autowired
	private UserInfoDao userinfoDao;

	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> list(ProjectEntity projectEntity,
			@RequestParam(name = "page", defaultValue = "1") Integer pageNum,
			@RequestParam(name = "rows", defaultValue = "20") Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<ProjectEntity> list = projectMappers.projectList(projectEntity);
		System.out.println("项目结果集：" + projectEntity);
		PageInfo<ProjectEntity> info = new PageInfo<>(list);
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}

	@PostMapping("save")
	@ResponseBody
	@Transactional
	public Map<String, Object> save(HttpServletRequest request, Model model ) {
		System.out.println("projectEntiDDDDDDDDDDD!!!!!:");
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
	 String projectType=request.getParameter("projectType");
	 String projectId=request.getParameter("projectId");
	 String userId=request.getParameter("userId");
	 System.out.println("sdfds类型："+projectType);
	 UserInfoEntity userInfoEntity=userinfoDao.findOne(Long.valueOf(userId));
			ProjectEntity project;
			if ( projectId== null||"".equals(projectId)) {
				// 添加
				project = new ProjectEntity();
				project.setDelete(1);
				project.setCreaTime(new Date());
				project.setUserinfo(userInfoEntity);
				project.setProjectData("暂无");
				project.setTeamSize(0);
			} else {
				// 编辑
				project = projectDao.findOne(Long.valueOf(projectId));
				project.setProjectId(Long.valueOf(projectId));
			}
			project.setProjectType(projectType);
			projectDao.save(project);
			obj.put("success", true);
		return obj;
	}

	@RequestMapping("/delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String, Object> delete(@PathVariable("id") Long id) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		ProjectEntity project = projectDao.findOne(id);
		if (id != null && project != null) {
			System.out.println("id AAA:"+id);
			project.setDelete(0);// 删除项目表中的内容
			List<TeamEntity> team = teamDao.findByProjectId(id);
			for (TeamEntity teamEntity : team) {
				teamEntity.setDelete(0);
				teamDao.save(teamEntity);
			}
			projectDao.save(project);
			obj.put("success", true);
		}else {
			obj.put("message", "你要删除的记录不存在！");
		}
		return obj;
	}
}
